<?php
declare(strict_types=1);

namespace Modules\Admin\Service;

use HiCommon\Helper;
use HiCommon\Repository\CheckpointTypeRepository;
use HiCommon\Repository\ShopRepository;

class CheckpointTypeService
{
    /**
     * 检测机构类型列表
     */
    public function checkpointList($where)
    {
        $data = CheckpointTypeRepository::get_list($where);

        $data->appends($where);//添加链接参数
        $list = list_page_data($data, $where);

        return $list;
    }


    /**
     * 添加
     */
    public function addOne(array $arrInput)
    {
        try {
            $res = CheckpointTypeRepository::add_one($arrInput);
            if (empty($res)) {
                return hello_error('添加失败');
            }

            return hello_success('添加成功', $res);
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     * 获取一条数据
     */
    public function getOne(int $id)
    {
        return CheckpointTypeRepository::get_one($id);
    }

    /**
     * 修改一条数据
     */
    public function editOne(array $data)
    {
        $res = CheckpointTypeRepository::edit_one($data);
        if (empty($res)) {
            return hello_error('修改失败');
        }

        return hello_success('修改成功', $res);
    }

    /**
     * 删除
     */
    public function del($data)
    {
        try {
            if ($data <= 0) {
                return hello_error('数据id不能为空');
            }

            if (ShopRepository::get_where_one(['type' => $data])) {
                return hello_error('该类型已被使用不能删除');
            }

            $res = CheckpointTypeRepository::delete_one($data);
            if ($res === false) {
                return hello_error('操作失败');
            }
            return hello_success('成功');

        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }


}